package com.mymarkdown.app.fx.event.hander.impl;

import com.mymarkdown.app.fx.FxApplication;
import com.mymarkdown.app.fx.annotation.KeyWord;
import com.mymarkdown.app.fx.enums.FxEventEnum;
import com.mymarkdown.app.fx.event.hander.KeyWordHandler;
import com.mymarkdown.app.fx.event.hander.MenuHandler;
import com.mymarkdown.app.fx.event.model.FxEvent;
import com.mymarkdown.app.interfaces.ModifyWindow;
import com.mymarkdown.app.interfaces.UserInfoService;
import com.mymarkdown.app.util.CurrentUserUtil;
import javafx.scene.web.WebEngine;
import javafx.stage.DirectoryChooser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Objects;

/**
 * @author ：zenglun
 * @date ：Created in 2021/12/28 7:33 PM
 * @description：
 * @modified By：
 * @version: $
 */
@Slf4j
@Component
@KeyWord(FxEventEnum.SETTING_CURRENT_WORK_DIR)
public class SettingCurrentWorkDirMenuHandlerImpl  implements KeyWordHandler, MenuHandler {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private ModifyWindow modifyWindow;

    @Override
    public void handler(WebEngine engine, FxEvent event) {
        DirectoryChooser directoryChooser = new DirectoryChooser();
        directoryChooser.setTitle("请选择要打开的工作空间");
        File file = directoryChooser.showDialog(FxApplication.getPrimaryStage());
        if (Objects.nonNull(file) && file.exists()){
            userInfoService.setWorkDir(file);
            modifyWindow.loadWorkDirTree();
        }
    }

    @Override
    public boolean afterHandler(WebEngine engine, FxEvent event) {
        return false;
    }

    @Override
    public void beforeHandler(WebEngine engine, FxEvent event) {

    }
}
